<?php
/**
 * ChangepassController
 * 
 * @author
 * @version 
 */
require_once 'Zend/Controller/Action.php';
class User_ChangepassController extends Zend_Controller_Action
{
    public function init() {	
		//set layout
    	$layoutPath = APPLICATION_PATH  . '/templates/user/user';
		$option = array (	'layout' 		=> 'index', 
              	        'layoutPath' 	=> $layoutPath );
		Zend_Layout::startMvc ( $option );
	}
    public function indexAction ()
    {
       Zend_Loader::loadClass("AuthModel");
       Zend_Loader::loadClass("UserModel");
       Zend_Loader::loadClass("LogModel");
       Zend_Loader::loadClass("LogLine");
        
       $logModel = new LogModel();
       $logLine = $logModel->getLogLine();
       
        
       $user = new UserModel();
       $authModel = new AuthModel();
       $view = $this->view;
       $user_id = $authModel->getUserInfo("id");
       $username = $authModel->getUserInfo("username");
       $pwd = $authModel->getUserInfo("password");
       
        $request = $this->getRequest();
        $view->callback = $request->getParam("callback");
        
        if ($request->isPost()) {
            $newpass = $request->getParam("newpass");
           	$newpass_confirm = $request->getParam("newpass_confirm");
           	$oldpassword = hash("SHA1", $request->getParam("oldpass"));
            $password = hash("SHA1", $newpass);
            if (strcmp($pwd,$oldpassword) == 0) {
            	if(strcmp($newpass, $newpass_confirm)==0){
            		
                	$data = array('password' => $password);
                	$user->updateUserData($user_id, $data);
                	$msg = "パスワード更新が成功しまた";
            		$view->msg = $msg;
                	
            		//ログ
                	$logLine->setType(LogLine::TYPE_OPERATION);
                	$logLine->addParts($user_id);
                	$logLine->addParts($username);
                	$logLine->addParts($oldpassword);
                	$logLine->addParts($password);
                	$logLine->setTime();
                	$logModel->addLogLine($logLine);
                	$logModel->flush();
                	
            	}else{
            		$err_msg = "新しいパスワードを確認ください";
            		$view->err_msg = $err_msg;
            	}
       			
       		}else{
       			//ログ
       			$logLine->setType(LogLine::TYPE_ERROR);
                $logLine->addParts($user_id);
                $logLine->addParts($username);
                $logLine->addParts($oldpassword);
                $logLine->addParts($password);
                $logLine->setTime();
                $logModel->addLogLine($logLine);
                $logModel->flush();
                
       			$err_msg = "古いパスワードが違う";
       			$view->err_msg = $err_msg;
       		}
       }
       
    }
}
